package controller;

import java.io.IOException;
import java.util.ArrayList;

import VMO.MealVMO;
import XMLUtilities.StAXUtilities;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MenuClientServlet
 */
public class MenuClientServlet extends HttpServlet {

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MenuClientServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String day = request.getParameter("day");
		String mealNum = request.getParameter("mealNum");
		String dayNum = request.getParameter("dayNum");

		String realPath = getServletContext().getRealPath("/");
		String xmlFilePath = realPath + "WEB-INF/xml/plan.xml";

		int intDay = Integer.parseInt(day);
		int intMealNum = Integer.parseInt(mealNum);
		int mealOrderMin = (intDay - 1) * (intMealNum);
		int mealOrderMax = intDay * intMealNum - 1;

		String strMealOrderMin = String.valueOf(mealOrderMin);
		String strMealOrderMax = String.valueOf(mealOrderMax);

		ArrayList<MealVMO> mealList = new ArrayList<MealVMO>();
		mealList = StAXUtilities.findDishList(xmlFilePath, id, strMealOrderMin,
				strMealOrderMax);

		request.setAttribute("MEALLIST", mealList);
		request.setAttribute("DAYNUM", dayNum);
		RequestDispatcher rd = request.getRequestDispatcher("menu-details.jsp");
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
